﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace NopSolutions.NopCommerce.Controls.Adapters
{
    public class MenuAdapter : System.Web.UI.WebControls.Adapters.MenuAdapter
    {
        Menu _menu = null;
        MenuItem _item = null;

        protected override void RenderContents(HtmlTextWriter writer) {
            _menu = Control as Menu;

            // ul
            writer.WriteBeginTag("ul");
            writer.WriteAttribute("class", "nav navbar-nav");
            writer.WriteAttribute("id", this.Control.ClientID);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Indent++;            

            foreach (MenuItem item in Control.Items) {
                writer.WriteLine();

                // list item
                writer.WriteBeginTag("li");

                if (item.Selected) {
                    writer.WriteAttribute("class", "active");
                }

                if (item.ChildItems != null && item.ChildItems.Count > 0) {
                    writer.WriteAttribute("class", "dropdown");
                }

                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Indent++;
                writer.WriteLine();

                writer.WriteBeginTag("a");

                if (!String.IsNullOrEmpty(item.NavigateUrl)) {
                    writer.WriteAttribute("href", Page.Server.HtmlEncode(_menu.ResolveClientUrl(item.NavigateUrl)));
                }
                else {
                    writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(_menu, "b" + item.ValuePath.Replace(_menu.PathSeparator.ToString(), "\\"), true));
                }

                if (!String.IsNullOrEmpty(item.ToolTip)) {
                    writer.WriteAttribute("title", item.ToolTip);
                    //writer.WriteAttribute("data-toggle", "tooltip");
                }

                if (item.ChildItems != null && item.ChildItems.Count > 0) {
                    writer.WriteAttribute("class", "dropdown-toggle");
                    writer.WriteAttribute("data-toggle", "dropdown");
                    writer.WriteAttribute("data-target", "#");
                }

                writer.Write(HtmlTextWriter.TagRightChar);

                writer.Indent++;
                writer.WriteLine();

                // item text
                writer.Write(item.Text);

                if (item.ChildItems != null && item.ChildItems.Count > 0) {
                    writer.WriteLine();

                    writer.WriteBeginTag("b");
                    writer.WriteAttribute("class", "caret");
                    writer.Write(HtmlTextWriter.TagRightChar);
                    writer.WriteEndTag("b");                    
                }

                writer.Indent--;
                writer.WriteLine();
                writer.WriteEndTag("a");

                writer.WriteLine();

                if (item.ChildItems != null && item.ChildItems.Count > 0) {
                    writer.WriteBeginTag("ul");
                    writer.WriteAttribute("class", "dropdown-menu");
                    writer.Write(HtmlTextWriter.TagRightChar);

                    writer.Indent++;                    

                    // lis
                    foreach (MenuItem childItem in item.ChildItems) {
                        writer.WriteLine();

                        _item = null;
                        BuildItem(childItem, writer);
                    }

                    writer.Indent--;
                    writer.WriteLine();
                    writer.WriteEndTag("ul");
                }

                // end list item
                writer.Indent--;
                writer.WriteLine();
                writer.WriteEndTag("li");                
            }
            // end ul
            writer.Indent--;
            writer.WriteLine();
            writer.WriteEndTag("ul");

            writer.WriteLine();
        }

        private void BuildItem(MenuItem item, HtmlTextWriter writer) {
            if (item.ChildItems != null && item.ChildItems.Count > 0) {
                // divier
                writer.WriteBeginTag("li");
                writer.WriteAttribute("class", "divider");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteEndTag("li");

                // header
                writer.WriteBeginTag("li");
                writer.WriteAttribute("class", "dropdown-header");
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write(item.Text.ToUpper());
                writer.WriteEndTag("li");

                _item = item;

                foreach (MenuItem childItem in item.ChildItems) {
                    BuildItem(childItem, writer);
                }                
            }
            else {
                if (_item != null && _item != item && _item != item.Parent && _item.ChildItems.Count > 0) {
                    writer.WriteBeginTag("li");
                    writer.WriteAttribute("class", "divider");
                    writer.Write(HtmlTextWriter.TagRightChar);
                    writer.WriteEndTag("li");

                    _item = item;
                }

                // list item
                writer.WriteBeginTag("li");

                if (item.Selected) {
                    writer.WriteAttribute("class", "active");
                }

                writer.Write(HtmlTextWriter.TagRightChar);

                writer.Indent++;
                writer.WriteLine();

                // item is a link
                if (IsLink(item)) {
                    writer.WriteBeginTag("a");

                    if (!String.IsNullOrEmpty(item.NavigateUrl)) {
                        writer.WriteAttribute("href", Page.Server.HtmlEncode(_menu.ResolveClientUrl(item.NavigateUrl)));
                    }
                    else {
                        writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(_menu, "b" + item.ValuePath.Replace(_menu.PathSeparator.ToString(), "\\"), true));
                    }

                    if (!String.IsNullOrEmpty(item.ToolTip)) {
                        writer.WriteAttribute("title", item.ToolTip);
                    }

                    writer.Write(HtmlTextWriter.TagRightChar);
                }
                else {
                    // item is plain text
                    writer.WriteBeginTag("p");
                    writer.WriteAttribute("class", "navbar-text");
                }

                writer.Indent++;
                writer.WriteLine();

                // item text
                writer.Write(item.Text);

                writer.Indent--;
                writer.WriteLine();

                // it' a link!
                if (IsLink(item)) {
                    writer.WriteEndTag("a");
                }
                else {
                    writer.WriteEndTag("p");
                }

                writer.Indent--;
                writer.WriteLine();

                // end list item
                writer.WriteEndTag("li");                
            }
        }

        protected virtual bool IsLink(MenuItem item) {
            return (item != null) && item.Enabled && ((!String.IsNullOrEmpty(item.NavigateUrl)) || item.Selectable);
        }
    }
}